package cn.wolfcode.trip.base.service.impl;


import cn.wolfcode.trip.base.domain.Praise;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.TagMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TagQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService{
    @Autowired
    private StrategyCommentMapper strategyCommentMapper;
    @Autowired
    private TagMapper tagMapper;

    @Override
    public void saveOrUpdate(StrategyComment strategyComment, String[] tags) {
        Date date = new Date();
        Tag tag = new Tag();

        if(strategyComment.getState() == StrategyComment.STATE_COMMEND){
            strategyComment.setCommendTime(date);
        }
        if(strategyComment.getId() == null){
            strategyComment.setCreateTime(date);
            strategyComment.setUser(UserContext.getUserAttribute());
            strategyCommentMapper.insert(strategyComment);
            for (String s : tags) {
                tag.setName(s);
                tagMapper.insert(tag);
                tagMapper.insertRelation(strategyComment.getId(),tag.getId());
            }
        }else {
            strategyCommentMapper.updateByPrimaryKey(strategyComment);
        }
    }



    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyComment> list = strategyCommentMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public PageInfo queryForTags(TagQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Map> list = tagMapper.queryForTags(qo);
        return new PageInfo(list);
    }

    @Override
    public List getUsers(Integer state) {

        return strategyCommentMapper.selectUsers(state);

    }

    @Override
    public StrategyComment getStrategyComment(Long strategyCommentId) {
        return strategyCommentMapper.selectByPrimaryKey(strategyCommentId);
    }

    @Override
    public List queryComments(Long userId) {
        List strategyComments=strategyCommentMapper.queryComments(userId);
        return strategyComments;
    }

    @Override
    public void insertCommentPraise(Praise obj) {
        strategyCommentMapper.insertCommentPraise(obj);
        strategyCommentMapper.updateAddNums(obj.getCommentId());
    }

    @Override
    public void deleteCommentPraise(Praise obj) {
        strategyCommentMapper.deleteCommentPraise(obj);
        strategyCommentMapper.updateSubNums(obj.getCommentId());
    }

    @Override
    public Boolean hasCommentPraise(Praise obj) {
        String s = strategyCommentMapper.hasCommentPraise(obj);
        if(s==null){
            return false;
        }
        return true;
    }


}
